<template>
  <div class="login-container">
    <div class="left">
      <div class="logo" />
      <div style="color: #ffffff; font-size: 64px" class="flex">
        <span>Hi,你好</span>
        <span>欢迎进入物流系统</span>
        <span style="color: #ffffff; font-size: 26px"
          >Welcome to the Logistics System</span
        >
      </div>
    </div>
    <div class="right">
      <span
        style="
          color: #2087d0;
          font-size: 42px;
          padding-bottom: 50px;
          font-weight: bold;
        "
        >Please Sign in</span
      >
      <div class="dis">
        <el-input
          ref="usernameInput"
          v-model.trim="loginForm.username"
          placeholder="请输入登录账号"
          @click="focusInput"
        >
          <i slot="prefix" class="el-input__icon el-icon-user" />
        </el-input>
        <el-input v-model.trim="loginForm.password" placeholder="请输入密码">
          <i slot="prefix" class="el-input__icon el-icon-lock" />
        </el-input>
      </div>
      <div class="f-flex">
        <el-checkbox v-model="checked">记住密码</el-checkbox>
        <el-link type="primary" @click="jump">忘记密码</el-link>
      </div>
      <el-button
        type="primary"
        class="btn"
        :loading="loading"
        @click.native.prevent="handleLogin"
        >登录</el-button
      >
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error("用户名的长度不能小于5"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码的长度不能小于6"));
      } else {
        callback();
      }
    };
    return {
      checked: true,
      loginForm: {
        username: "admin",
        password: "admin123",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  watch: {
    // $route: {
    //   handler: function (route) {
    //     this.redirect = route.query && route.query.redirect;
    //   },
    //   immediate: true,
    // },
  },
  mounted() {},
  methods: {
    focusInput() {
      // 使用 $refs 来访问 el-input 元素，然后调用 focus 方法
      this.$refs.usernameInput.focus();
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    jump() {
      this.$router.replace({ path: "/forget" });
    },
    handleLogin() {
      this.loading = true;
      this.$store
        .dispatch("user/login", this.loginForm)
        .then(() => {
          this.$router.push({ path: this.redirect || "/" });
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: black;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

.login-container {
  height: 100%;
  background-image: url("../../static/beijin.png");
  background-size: 100% 100%;
}
.el-row {
  margin-bottom: 20px;
  display: flex;
  height: 100vh;
  justify-content: center;
  align-items: center;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
/* reset element-ui css */
.login-container {
  .el-input {
    display: flex;
    height: 56px;
    width: 422px;
    align-items: center;

    input {
      background: transparent;
      border: 1px solid #f0f1f5;
      -webkit-appearance: none;
      border-radius: 0px;
      color: $light_gray;
      height: 47px;
      caret-color: black;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  width: 100%;
  height: 100%;
  background-color: $bg;
  overflow: hidden;
  display: flex;
  flex-flow: row;
  justify-content: space-around;
  align-items: center;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .left {
    display: flex;
    flex-flow: column;
    .logo {
      width: 90px;
      height: 90px;
      background-image: url("../../static/logod.png");
      background-size: contain;
      background-position: center;
      background-repeat: no-repeat;
    }
    .flex {
      display: flex;
      flex-flow: column;
      justify-content: space-between;
    }
  }

  .dis {
    display: flex;
    flex-flow: column;
    height: 140px;
    justify-content: space-around;
  }
  .f-flex {
    display: flex;
    width: 422px;
    justify-content: space-between;
    margin-top: 10px;
  }
  .btn {
    width: 422px;
    height: 56px;
    margin-top: 50px;
  }
  .right {
    width: 550px;
    height: 540px;
    background: white;
    border-radius: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-flow: column;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
